package com.handpay.zztong.hp.accountcenter.activity;

import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.handpay.framework.ClientEngine;
import com.handpay.framework.SecureManager;
import com.handpay.framework.swiper.SwiperMode;
import com.handpay.framework.utils.CommonUtils;
import com.handpay.zztong.hp.R;
import com.handpay.zztong.hp.SwipeCard;
import com.handpay.zztong.hp.accountcenter.model.modifycard.ModifyCardModel;
import com.handpay.zztong.hp.adapter.BankAdapter;
import com.handpay.zztong.hp.adapter.ProvinceAdapter;
import com.handpay.zztong.hp.base.activity.ZZTongActivity;
import com.handpay.zztong.hp.base.callback.BusinessEngineListener;
import com.handpay.zztong.hp.base.model.banklist.Bank;
import com.handpay.zztong.hp.base.model.banklist.BankProvince;
import com.handpay.zztong.hp.bean.ActionBar;
import com.handpay.zztong.hp.bean.BankArea;
import com.handpay.zztong.hp.bean.ResponseBean;
import com.handpay.zztong.hp.config.ZZTConstant;
import com.handpay.zztong.hp.log.HPLog;
import com.handpay.zztong.hp.login.model.sendphonecode.SendAuthCodeModel;
import com.handpay.zztong.hp.protocol.BaseProtocolKey;
import com.handpay.zztong.hp.protocol.UCProtocolKey;
import com.handpay.zztong.hp.ui.HPBankAreaEditView;
import com.handpay.zztong.hp.ui.HPSubBankEditView;
import com.handpay.zztong.hp.ui.VerificationCodeButton;
import com.handpay.zztong.hp.utils.AccountUtils;

import java.lang.ref.WeakReference;
import java.util.Hashtable;
import java.util.List;

/**
 * @author dlfeng
 */
public class BankCardModifyActivity extends ZZTongActivity implements HPSubBankEditView.OnBankLisenter, HPBankAreaEditView.OnBankAreaLisenter {
    private static final String TAG = BankCardModifyActivity.class.getSimpleName();
    private TextView mName;
    private Spinner mMainSpinner;
    private HPSubBankEditView mSubBank;
    private Button SaveChanges;
    private EditText mCardNo;
    public Bank mMainBank = null;
    private Bank mSubSelectedBank = null;
    private BankAdapter mMainAdapter;
    private EditText mETMsgCode;
    private VerificationCodeButton mBtnMsgCode;
    private TextView mTvPhone;
    private Spinner mProvincesFlagSpinner;
    private HPBankAreaEditView mAreaFlag;
    private ProvinceAdapter mProvinceAdapter;
    private BankProvince mBankProvince = null;
    private View open_provinces_line, open_areaflag_line, open_subbank_line;
    private BankArea mSelectBankArea = null;
    private View mProvinceTable, mAreaTable, mSubBankTable;
    private String bankName = null;
    private static final int REQUEST_CARD = 1;
//    private AuthCodeBill authCodeBill;
    private TextWatcher mTextWatch = new TextWatcher() {

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(final Editable s) {
            if (null == mMainBank || Bank.INVALID_BANK_CODE.equals(mMainBank.getBankCode())) {
                String title = BankCardModifyActivity.this.getString(R.string.tip);
                String message = BankCardModifyActivity.this.getString(R.string.select_bank_tip);
                BankCardModifyActivity.this.showAlertDialog(BankCardModifyActivity.this, title, message);
            } else {
                refreshHandler.removeMessages(HANDLER_MSG_TYPE_QUERY_SUBBANKLIST);
                Message msg = refreshHandler.obtainMessage();
                msg.what = HANDLER_MSG_TYPE_QUERY_SUBBANKLIST;
                msg.obj = s.toString();
                refreshHandler.sendMessageDelayed(msg, 1000);
            }
        }
    };
    private TextWatcher mBankAreaTextWatch = new TextWatcher() {

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(final Editable s) {
            if (mMainBank.getProvinceFlag().equals("1") && (null == mBankProvince || TextUtils.isEmpty(mBankProvince.getBankProvinceId()))) {
                String title = BankCardModifyActivity.this.getString(R.string.tip);
                String message = BankCardModifyActivity.this.getString(R.string.select_bankProvince_tip);
                BankCardModifyActivity.this.showAlertDialog(BankCardModifyActivity.this, title, message);
            } else {
                refreshHandler.removeMessages(HANDLER_MSG_TYPE_QUERY_BANKAREALIST);
                Message msg = refreshHandler.obtainMessage();
                msg.what = HANDLER_MSG_TYPE_QUERY_BANKAREALIST;
                msg.obj = s.toString();
                refreshHandler.sendMessageDelayed(msg, 1000);
            }
        }
    };

    protected void onStop() {
        super.onStop();
        mSubBank.onStop();
    }

    private final static int HANDLER_MSG_TYPE_QUERY_SUBBANKLIST = 1001;
    private final static int HANDLER_MSG_TYPE_QUERY_BANKAREALIST = 1002;
    private final static int HANDLER_MSG_TYPE_QUERY_ALLCITYS = 1003;
    private RefreshHandler refreshHandler;

    private static class RefreshHandler extends Handler {

        private WeakReference<BankCardModifyActivity> mActivity;

        private RefreshHandler(BankCardModifyActivity mActivity) {
            this.mActivity = new WeakReference<BankCardModifyActivity>(mActivity);
        }

        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case HANDLER_MSG_TYPE_QUERY_SUBBANKLIST:
                    String s = (String) msg.obj;
                    if (null != s) {
                        s = s.trim();
                    }
                    BankCardModifyActivity c = mActivity.get();
                    if (null != c && !TextUtils.isEmpty(s)) {
                        c.querySubBankList(c.mMainBank.getBankCode(), s);
                    }
                    break;
                case HANDLER_MSG_TYPE_QUERY_BANKAREALIST:
                    String area = (String) msg.obj;
                    if (null != area) {
                        area = area.trim();
                    }
                    BankCardModifyActivity activity = mActivity.get();
                    if (null != activity && !TextUtils.isEmpty(area)) {
                        if (null != activity.mBankProvince) {
                            activity.queryAreaFlag(activity.mMainBank.getBankCode(), area, activity.mBankProvince.getBankProvinceId());
                        } else {
                            activity.queryAreaFlag(activity.mMainBank.getBankCode(), area, null);
                        }
                    }
                    break;
                case HANDLER_MSG_TYPE_QUERY_ALLCITYS://查询所有城市
                    BankCardModifyActivity modifyCashCard = mActivity.get();
                    if (null != modifyCashCard.mBankProvince) {
                        modifyCashCard.queryAreaFlag(modifyCashCard.mMainBank.getBankCode(), "", modifyCashCard.mBankProvince.getBankProvinceId());
                    } else {
                        modifyCashCard.queryAreaFlag(modifyCashCard.mMainBank.getBankCode(), "", null);
                    }
                    break;
                default:
                    break;
            }

        }
    }

    protected boolean isShownTabs() {
        return false;
    }

    @Override
    public ActionBar getActivityActionBar() {
        return new ActionBar(this.getString(R.string.ModifyCard), true);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_bank_card_modify);
        super.onCreate(savedInstanceState);
//        authCodeBill = new AuthCodeBill(this);
        //不让输入框自动弹出
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        mName = (TextView) findViewById(R.id.Payee);
        mCardNo = (EditText) findViewById(R.id.cardNO);

        Intent i = getIntent();
        mName.setText(i.getStringExtra(UCProtocolKey.KEY_NAME));
        mCardNo.setText(i.getStringExtra(UCProtocolKey.KEY_CARDNUM));

        mMainSpinner = (Spinner) findViewById(R.id.spinner1);
        mProvincesFlagSpinner = (Spinner) this.findViewById(R.id.opening_provinces_spinner);
        mAreaFlag = (HPBankAreaEditView) this.findViewById(R.id.opening_city_edit);
        mSubBank = (HPSubBankEditView) this.findViewById(R.id.sub_bank);
        mProvinceTable = this.findViewById(R.id.opening_provinces);
        mAreaTable = this.findViewById(R.id.opening_city_view);
        mSubBankTable = this.findViewById(R.id.opening_sub_bank);
        open_provinces_line = this.findViewById(R.id.opening_provinces_line);
        open_areaflag_line = this.findViewById(R.id.opening_city_line);
        open_subbank_line = this.findViewById(R.id.subBank_line);

        SaveChanges = (Button) findViewById(R.id.saveChangebt);
        SaveChanges.setOnClickListener(this);
        mMainAdapter = new BankAdapter(this);
        mMainSpinner.setAdapter(mMainAdapter);
        mMainSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

                mMainBank = (Bank) parent.getAdapter().getItem(position);
                HPLog.i("BankCardModifyActivity", "mMainBank:" + mMainBank.getBankName());
                if (mMainBank.getProvinceFlag().equals("1")) {
                    mProvinceTable.setVisibility(View.VISIBLE);
                    open_provinces_line.setVisibility(View.VISIBLE);
                    // mProvincesFlagSpinner.setSelection(Spinner.INVALID_POSITION);
                    queryBankProvinceFlag(mMainBank.getBankCode());
                } else {
                    mBankProvince = null;
                    mProvinceTable.setVisibility(View.GONE);
                    open_provinces_line.setVisibility(View.GONE);
                }
                if (mMainBank.getAreaFlag().equals("1")) {
                    mAreaTable.setVisibility(View.VISIBLE);
                    open_areaflag_line.setVisibility(View.VISIBLE);
                } else {
                    clearBankAreaInfo();
                    mAreaTable.setVisibility(View.GONE);
                    open_areaflag_line.setVisibility(View.GONE);
                }
                if (mMainBank.getSubBankFlag().equals("1")) {
                    mSubBankTable.setVisibility(View.VISIBLE);
                    open_subbank_line.setVisibility(View.VISIBLE);
                } else {
                    clearSubBankInfo();
                    mSubBankTable.setVisibility(View.GONE);
                    open_subbank_line.setVisibility(View.GONE);
                }
                Intent i = getIntent();
                String subBankName = i.getStringExtra(UCProtocolKey.KEY_SUBBANKNAME);
                if (!TextUtils.isEmpty(subBankName)) {
                    i.putExtra(UCProtocolKey.KEY_SUBBANKNAME, "");
                } else {
                    clearSubBankInfo();
                    mBankProvince = null;
                }
                String bankAreaName = i.getStringExtra(UCProtocolKey.KEY_AREANAME);
                if (!TextUtils.isEmpty(bankAreaName)) {
                    i.putExtra(UCProtocolKey.KEY_AREANAME, "");
                } else {
                    clearBankAreaInfo();
                    mBankProvince = null;
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                mMainBank = null;
                mBankProvince = null;
                mSelectBankArea = null;
                HPLog.i("onNothingSelected", "onNothingSelected");
                clearSubBankInfo();
                clearBankAreaInfo();
            }
        });

        mProvinceAdapter = new ProvinceAdapter(this);
        mProvincesFlagSpinner.setAdapter(mProvinceAdapter);
        mProvincesFlagSpinner.setOnItemSelectedListener(mSelectedProvinceListener);

        mAreaFlag.addTextChangedListener(mBankAreaTextWatch);
        mAreaFlag.setLisenter(this);

        mSubBank.setLisenter(this);
        mSubBank.addTextChangedListener(mTextWatch);

        refreshHandler = new RefreshHandler(this);

        mTvPhone = (TextView) findViewById(R.id.tv_phone);
        mTvPhone.setText(CommonUtils.formatPhone(AccountUtils.getAccountPhone(this)));
        mETMsgCode = (EditText) findViewById(R.id.editText_SMS_verifyCode);
        mBtnMsgCode = (VerificationCodeButton) findViewById(R.id.button_send_verifycode);
        mBtnMsgCode.setOnClickListener(this);
        findViewById(R.id.swipeCard).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goToVPos();
            }
        });
        queryMainBankList(false);

    }

    private void cleanBankInfo() {
        mCardNo.setText("");
        mSubSelectedBank = new Bank();
        mSubBank.setText("");
        mBankProvince = new BankProvince();
        mSelectBankArea = new BankArea();
        mAreaFlag.setText("");
    }

    private void autoFillTwo() {

        Intent i = getIntent();
        String mSubBankId = i.getStringExtra(UCProtocolKey.KEY_SUBBANKCODE);
        String mSubBankName = i.getStringExtra(UCProtocolKey.KEY_SUBBANKNAME);
        String provinceId = i.getStringExtra(UCProtocolKey.KEY_PROVIVCEID);
        String provinceName = i.getStringExtra(UCProtocolKey.KEY_PROVINCENAME);
        String areaId = String.valueOf(i.getStringExtra(UCProtocolKey.KEY_AREAID));
        String areaName = i.getStringExtra(UCProtocolKey.KEY_AREANAME);
        HPLog.e("jjyang", "areaId:" + areaId);
        HPLog.e("jjyang", "areaName:" + areaName);
        String subBankCode = i.getStringExtra(UCProtocolKey.KEY_SUBBANKCODE);
        String subBankName = i.getStringExtra(UCProtocolKey.KEY_SUBBANKNAME);

        if (!TextUtils.isEmpty(mSubBankId) && !TextUtils.isEmpty(mSubBankName)) {
            mSubSelectedBank = new Bank();
            mSubSelectedBank.setBankCode(mSubBankId);
            mSubSelectedBank.setBankName(mSubBankName);
        }
        HPLog.e("jjyang", "ModifyCashCashCard:" + subBankName);
        if (!TextUtils.isEmpty(areaId) && !TextUtils.isEmpty(areaName)) {
            mSelectBankArea = new BankArea();
            mSelectBankArea.setBankAreaId(Integer.parseInt(areaId));
            mSelectBankArea.setBankAreaName(areaName);
        }

        if (!TextUtils.isEmpty(provinceId) && !TextUtils.isEmpty(provinceName)) {
            mProvinceTable.setVisibility(View.VISIBLE);
            open_provinces_line.setVisibility(View.VISIBLE);
        }
        if (!TextUtils.isEmpty(areaId) && !TextUtils.isEmpty(areaName)) {
            mAreaTable.setVisibility(View.VISIBLE);
            open_areaflag_line.setVisibility(View.VISIBLE);
            mAreaFlag.setText(areaName);
        }
        if (!TextUtils.isEmpty(subBankCode) && !TextUtils.isEmpty(subBankName)) {
            mSubBankTable.setVisibility(View.VISIBLE);
            open_subbank_line.setVisibility(View.VISIBLE);
            mSubBank.setText(subBankName);
        }
    }

    private OnItemSelectedListener mSelectedProvinceListener = new OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            mBankProvince = (BankProvince) parent.getAdapter().getItem(position);
            Intent i = getIntent();
            String bankProvinceName = i.getStringExtra(UCProtocolKey.KEY_PROVINCENAME);
            HPLog.e("mSelectedProvince", "bankAreaName:" + bankProvinceName + " postion:" + position + "  mSelectedProvince:" + mBankProvince.getBankProvinceName());
            //修改：选择了省份之后获城市列表
            if ("1".equals(mMainBank.getProvinceFlag())) {
                Message msg = refreshHandler.obtainMessage();
                msg.what = HANDLER_MSG_TYPE_QUERY_ALLCITYS;
                msg.obj = "";//空字符代表返回省下面的全部城市
                refreshHandler.sendMessage(msg);
                HPLog.e("tag", "发送消息成功");
            }
            if (!TextUtils.isEmpty(bankProvinceName)) {
                i.putExtra(UCProtocolKey.KEY_PROVINCENAME, "");
                i.putExtra(UCProtocolKey.KEY_PROVIVCEID, "");
            } else {
                clearBankAreaInfo();
            }
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {
            mSelectBankArea = null;
            clearBankAreaInfo();
        }
    };

    private void clearBankAreaInfo() {
        mAreaFlag.removeTextChangedListener(mBankAreaTextWatch);
        mAreaFlag.setText("");
        mSelectBankArea = null;
        mAreaFlag.addTextChangedListener(mBankAreaTextWatch);
    }

    private void clearSubBankInfo() {
        mSubBank.removeTextChangedListener(mTextWatch);
        mSubBank.setText("");
        mSubSelectedBank = null;
        mSubBank.addTextChangedListener(mTextWatch);
    }

//    @Override
//    protected void onStart() {
//        super.onStart();
//        if (0 == mMainAdapter.getCount()) {
//        }
//    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        int i = v.getId();
        if (i == R.id.saveChangebt) {
            String cardNo = this.mCardNo.getText().toString();
            cardNo = cardNo.replace(" ", "");
            if (!CommonUtils.isLegalCard(cardNo)) {
                Toast.makeText(this, R.string.input_right_card_no_tip, Toast.LENGTH_SHORT).show();
                this.mCardNo.requestFocus();
                return;
            } else if (TextUtils.isEmpty(mETMsgCode.getText().toString())) {
                Toast.makeText(this, R.string.input_right_message_code_tip, Toast.LENGTH_SHORT).show();
                return;
            }
            // 检查收款行
            if (null == mMainBank || "-1".equals(mMainBank.getBankCode())) {
                Toast.makeText(this, R.string.select_bank_tip, Toast.LENGTH_SHORT).show();
                return;
            }
            // 检查省份
            if ("1".equals(mMainBank.getProvinceFlag())) {
                if (TextUtils.isEmpty(mBankProvince.getBankProvinceId())) {
                    Toast.makeText(this, R.string.select_bankProvince_tip, Toast.LENGTH_SHORT).show();
                    return;
                }
            }
            // 检查地市
            if ("1".equals(mMainBank.getAreaFlag()) && null == mSelectBankArea) {
                if (TextUtils.isEmpty(mAreaFlag.getText().toString())) {
                    Toast.makeText(this, R.string.select_bankArea_tip, Toast.LENGTH_SHORT).show();
                    return;
                }
            }
            // 检查收款支行/开户行
            if ("1".equals(mMainBank.getSubBankFlag()) && null == mSubSelectedBank) {
                if (TextUtils.isEmpty(this.mSubBank.getText().toString())) {
                    Toast.makeText(this, R.string.select_sub_bank_tip, Toast.LENGTH_SHORT).show();
                    return;
                }
            }
            String message = mETMsgCode.getText().toString();
            if (null != message) {
                message = message.trim();
            }
            if (TextUtils.isEmpty(message)) {
                Toast.makeText(this, R.string.input_right_message_code_tip, Toast.LENGTH_SHORT).show();
                return;
            }
            saveCard();
        } else if (i == R.id.button_send_verifycode) {
            zztSendPhoneCode();
        }
    }

    private void zztSendPhoneCode() {
        if (!CommonUtils.isMobileNum(AccountUtils.getAccountPhone(this))) {
            Toast.makeText(this, R.string.input_right_phone_tip, Toast.LENGTH_SHORT).show();
            return;
        }
        new SendAuthCodeModel().doSendAuthCodePost(this, ZZTConstant.AUTHCODE_ACTION_MODIFYCARD,  AccountUtils.getAccountPhone(this),new BusinessEngineListener<ResponseBean>(){
            @Override
            public void callbackBusinessResponse(ResponseBean bean) {
                if (bean.getResponseCode().equals("0")) {
                    Toast.makeText(BankCardModifyActivity.this, "发送验证码成功！", Toast.LENGTH_SHORT).show();
                } else {
                    showAlertDialog(BankCardModifyActivity.this, getString(R.string.prompt), bean.getErrMessage());
                }
            }
        });
        mBtnMsgCode.startLocker(-1);
    }

    private boolean saveCard() {
        Hashtable<String, String> p = new Hashtable<String, String>();
        String phone = AccountUtils.getAccountPhone(this);
        if (TextUtils.isEmpty(phone)) {
            HPLog.e(TAG, "phone null");
            return false;
        }
        String subBankCode = null;
        if (null != mSubSelectedBank) {
            subBankCode = mSubSelectedBank.getBankCode();
        }
        String provinceId = null;
        if (null != mBankProvince) {
            provinceId = mBankProvince.getBankProvinceId();
        }
        String areaId = null;
        if (null != mSelectBankArea) {
            areaId = String.valueOf(mSelectBankArea.getBankAreaId());
        }

        String cardNo = mCardNo.getText().toString();
        cardNo = cardNo.replace(" ", "");
        String bankCode = mMainBank.getBankCode();
        String channel = ClientEngine.CHANNEL;

        p.put(UCProtocolKey.PHONE, SecureManager.getInstance().des(phone, 1, null));
        p.put(UCProtocolKey.KEY_NAME, SecureManager.getInstance().des(mName.getText().toString(), 1, null));
        p.put(UCProtocolKey.KEY_CARDNUM, SecureManager.getInstance().des(cardNo, 1, null));
        p.put(UCProtocolKey.ACCOUNT, SecureManager.getInstance().des(AccountUtils.getAccountName(this), 1, null));
        p.put(UCProtocolKey.KEY_BANKCODE, bankCode);
        if (mMainBank.getSubBankFlag().equals("1") && !TextUtils.isEmpty(subBankCode)) {
            p.put(UCProtocolKey.KEY_SUBBANKCODE, subBankCode);
        }
        if (mMainBank.getProvinceFlag().equals("1") && !TextUtils.isEmpty(provinceId)) {
            p.put(UCProtocolKey.KEY_PROVIVCEID, provinceId);
        }
        if (mMainBank.getAreaFlag().equals("1") && !TextUtils.isEmpty(areaId)) {
            p.put(UCProtocolKey.KEY_AREAID, areaId);
        }
        p.put(UCProtocolKey.CHANNEL, channel);
        p.put(BaseProtocolKey.KEY_VERIFYCODE, mETMsgCode.getText().toString());
        p.put(BaseProtocolKey.KEY_ACTION, ZZTConstant.AUTHCODE_ACTION_MODIFYCARD);
        p.put(BaseProtocolKey.NEWBANKFLAG, newBankFlag);
//        this.doJsonPost(this, DO_zztModifyCardNum, ResponseBean.class, p);
        new ModifyCardModel().doModifyCardPost(this, p, new BusinessEngineListener<ResponseBean>() {
            @Override
            public void callbackBusinessResponse(ResponseBean bean) {
                showAlertDialog(BankCardModifyActivity.this, getString(R.string.tip), getString(R.string.update_ModifyCashCard_Success), false, AlertOKButton);
            }
        });
        return true;
    }

    @Override
    public void onBanksLoad(List<Bank> banks) {
        String bankCode = getIntent().getStringExtra(UCProtocolKey.KEY_BANKCODE);
        HPLog.e("jjyang", "BankCardModifyActivity:" + bankCode);
        mMainAdapter.refreshBanks(banks);
        mMainSpinner.setAdapter(mMainAdapter);
        if (!TextUtils.isEmpty(bankCode)) {
            int i;
            for (i = 0; i < banks.size(); i++) {
                if (banks.get(i).getBankCode().equals(bankCode)) {
                    break;
                }
            }
            if (i == banks.size()) {
                i = 0;
                cleanBankInfo();
            } else {
                autoFillTwo();
            }
            mMainBank = banks.get(i);
            mMainSpinner.setSelection(i);
            getIntent().putExtra(UCProtocolKey.KEY_BANKCODE, "");
        } else {
            mMainSpinner.setSelection(mMainAdapter.getCount());
        }

//        else if (Do_ZztSubBankList.equals(action)) {
//            mSubBank.refreshBanks(banks);
//        }
    }

    @Override
    public void onSubBanksLoad(List<Bank> banks) {
        mSubBank.refreshBanks(banks);
    }

    @Override
    public void onBankProvincesLoad(List<BankProvince> province) {
//        if (Do_ZztBankProvinceList.equals(action)) {
        requestCurrentLocation(true);
        String bankProvinceid = getIntent().getStringExtra(UCProtocolKey.KEY_PROVIVCEID);
        mProvinceAdapter.refreshBankProvinces(province);
        mProvincesFlagSpinner.setAdapter(mProvinceAdapter);
        if (!TextUtils.isEmpty(bankProvinceid)) {
            int j;
            for (j = 0; j < province.size(); j++) {
                if (province.get(j).getBankProvinceId().equals(bankProvinceid)) {
                    break;
                }
            }
            if (j == province.size()) {
                j = 0;
            }
            mBankProvince = province.get(j);
            mProvincesFlagSpinner.setSelection(j);
            getIntent().putExtra(UCProtocolKey.KEY_PROVIVCEID, "");
        } else {
            mProvincesFlagSpinner.setSelection(mProvinceAdapter.getCount());
            // mBankProvince = province.get(0);
        }
//        }
    }

    @Override
    public void onBankAreaLoad(List<BankArea> area) {

        mAreaFlag.refreshBankArea(area);

    }



    /**
     *
     */
    OnClickListener AlertOKButton = new OnClickListener() {
        @Override
        public void onClick(DialogInterface arg0, int arg1) {
            // Intent intent=new
            // Intent(BankCardModifyActivity.this,AccountActivity.class);
            // BankCardModifyActivity.this.startActivity(intent);
            arg0.dismiss();
            BankCardModifyActivity.this.finish();
        }
    };

    @Override
    public void onBankSelected(Bank b) {
        //
        mSubBank.removeTextChangedListener(mTextWatch);
        if (null != b) {
            this.mSubSelectedBank = b;
            mSubBank.setText(b.getBankName());
        } else {
            this.mSubSelectedBank = null;
            mSubBank.setText("");
        }
        mSubBank.addTextChangedListener(mTextWatch);
    }

    @Override
    public void onBankAreaSelected(BankArea area) {
        // 在用户选中的瞬间，是不用去搜索的。所以先去掉监听，设置完成后，再加入监听。
        mAreaFlag.removeTextChangedListener(mBankAreaTextWatch);
        if (null != area) {
            this.mSelectBankArea = area;
            mAreaFlag.setText(area.getBankAreaName());
        } else {
            this.mSelectBankArea = null;
            mAreaFlag.setText("");
        }
        mAreaFlag.addTextChangedListener(mBankAreaTextWatch);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (REQUEST_CARD == requestCode) {
            if (RESULT_OK == resultCode && null != data) {
                mCardNo.setText(data.getStringExtra(BaseProtocolKey.ACCOUNT));
            }
        } else {
            super.onActivityResult(requestCode, resultCode, data);
        }
    }

    @Override
    public void goToVPos() {
        super.goToVPos();
        setMode(SwiperMode.SUPER_TRANS, true, new Runnable() {

            @Override
            public void run() {
                Intent intent = new Intent(BankCardModifyActivity.this, SwipeCard.class);
                startActivityForResult(intent, REQUEST_CARD);
            }
        });
    }
}
